using System;
using System.IO;
using System.Windows.Forms;
using Jabr.GUI.Forms;
using Jabr.Logic.Exceptions.Handling;
using Jabr.Logic.Roster;
using Jabr.Logic.Serialization;

namespace Jabr
{
    static class Program
    {
        static private Form m_MainForm;
        static private Exception m_MainFormCtorException;

        static private void InitRoster()
        {
            try
            {
                try
                {
                    StreamReader rs = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Jabr\accounts.xml");
                    RosterSingleton.Instance = (RosterSingleton)Serializer.Instance.Deserialize(rs.ReadToEnd());
                    rs.Close();
                }
                catch (DirectoryNotFoundException e)
                {
                    Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Jabr");
                    throw new RosterException("Application settings directory not found", e);
                }
                catch (FileNotFoundException e)
                {
                    throw new RosterException("Roster file not found", e);
                }
            }
            catch (RosterException)
            {
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Invalid accounts definition file. Application will now exit.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Environment.Exit(-1);
            }
        }
        static private void DeinitRoster()
        {
            if (RosterSingleton.Instance.AccountCollection.Count != 0)
            {
                StreamWriter os = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Jabr\accounts.xml", false); ;
                os.Write(Serializer.Instance.Serialize(RosterSingleton.Instance));
                os.Close();
            }
        }

        static private void InitApp()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                m_MainForm = RosterForm.Instance;
                m_MainFormCtorException = null;
            }
            catch (Exception e)
            {
                m_MainFormCtorException = e;
            }
            AbstractExceptionHandler eh = ExceptionHandlerFactory.GetHandler(m_MainForm);
            AppDomain.CurrentDomain.UnhandledException += eh.CurrentDomain_UnhandledException;
            Application.ThreadException += eh.Application_ThreadException;
            if (m_MainFormCtorException != null)
                eh.HandleException(m_MainFormCtorException);
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            Program.InitRoster();
            Program.InitApp();
            Application.Run(m_MainForm);
        }

        static void Application_ApplicationExit(object sender, EventArgs e)
        {
            DeinitRoster();
        }
    }
}